'''
  This Source Code Form is subject to the terms of the Mozilla Public
  License, v. 2.0. If a copy of the MPL was not distributed with this
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
'''
from swe.log import log
from conf import time as conf
def formatedSysTime():
    import time
    raw = time.localtime()
    handled = []
    for i in raw:
        handled.append(str(i))
    del raw
    return handled[0] + "-" + handled[1] + "-" + handled[2] + " " + handled[3] + ":" + handled[4] + ":" + handled[5]

def syncSysTime():
    import ntptime
    from time import localtime
    from machine import RTC
    rtc = RTC()
    ntptime.source = conf.ntpSource
    ntptime.timeout = conf.ntpTimeOut
    try:
        rtc.init(localtime(ntptime.time() + 8 * 3600))
    except Exception as err:
        log("fatal", "Sys::Setup::timeSync", "Failed to synchronize time! Detected err: " + repr(err), if_print = True)
        syncSysTime()
    log("debug", "Sys::Setup::timeSync", "Time synchronized successfully.", if_print = False)
    
def generateForDisplay():
    import time
    L = str(time.localtime()[3])
    R = str(time.localtime()[4])
    if len(L) < 2:
        L = "0" + L
    if len(R) < 2:
        R = "0" + R
    return L + R